package cqupt.spark_log.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cqupt.spark_log.utils.ECUtil;

public class Job {
	private String jobId;
	private Date starTime;
	private Date endTime;
	private double ec;
	private List<Stage> stages = new ArrayList<Stage>();
	
	public Job(String jobId, Date starTime) {
		this.jobId = jobId;
		this.starTime = starTime;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public Date getStarTime() {
		return starTime;
	}

	public void setStarTime(Date starTime) {
		this.starTime = starTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public void addStage(Stage stage) {
		this.stages.add(stage);
	}

	public List<Stage> getStages() {
		return stages;
	}
	
	public double getEc() {
		if (ec<=0) {
			for (int i=0; i<stages.size(); i++) {
				Stage stage = stages.get(i);
				ec += stage.getEc();
			}
		}
		ec = ECUtil.get4point(ec);
		System.out.println("job"+this.jobId+"能量为["+ec+"]");
		return ec;
	}
	
	public long getRuntime() {
		long result = 0;
		//job下面的task也是串行的
		for (Stage stage: stages) {
			result += stage.getRuntime();
		}
		return result;
	}

	public void setEc(double ec) {
		this.ec = ec;
	}

	@Override
	public String toString() {
		return "Job [jobId=" + jobId + ", starTime=" + starTime + ", endTime=" + endTime + ", ec=" + ec + "]";
	}
	//得到指定阶段的能耗
	public double getEc(String stageName) {
		String[] jobAndStage = stageName.split(";");
		if (ec<=0) {
			for (int i=0; i<stages.size(); i++) {
				Stage stage = stages.get(i);
				if (stage.getStageId().equals(jobAndStage[1])) {
					ec += stage.getEc();
					break;
				}
			}
		}
		ec = ECUtil.get4point(ec);
		return ec;
	}

	public long getStageTime(String stageName) {
		String[] jobAndStage = stageName.split(";");
		for (int i=0; i<stages.size(); i++) {
			Stage stage = stages.get(i);
			if (stage.getStageId().equals(jobAndStage[1])) {
				return stage.getRuntime();
			}
		}
		return 0;
	}
	
}
